package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

public class U_recyleview_adapter extends RecyclerView.Adapter<U_recyleview_adapter.ViewHolder>{
    private final Context context;
    int pl_numbers;
    List<VideoEntity> videoEntities=new ArrayList<>();

    //构造方法
    public U_recyleview_adapter(Context context, int pl_numbers, boolean ispage1){
        this.context=context;
        this.pl_numbers=pl_numbers;
    }


    public void setData(List<VideoEntity> videoEntities){
        this.videoEntities=videoEntities;
        notifyDataSetChanged();
    }

    public List<VideoEntity> getData(){
        return this.videoEntities;
    }
    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        RoundedImageView roundedImageView;
        TextView textView_titile;
        TextView textView_1;

        TextView textView_2;

        TextView textView_3;

        private View list_video;

        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            roundedImageView = (RoundedImageView) view.findViewById(R.id.video_face);
            textView_titile = view.findViewById(R.id.about_v_title);
            textView_1 = (TextView) view.findViewById(R.id.about_username);
            textView_2 = (TextView) view.findViewById(R.id.about_bfsl);
            textView_3 = (TextView) view.findViewById(R.id.about_plsl);
            list_video = view.findViewById(R.id.list_main_body);
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        view= LayoutInflater.from(context).inflate(R.layout.list_aboutvideo,parent,false);
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(Uri.parse(videoEntities.get(position).getVideo_cover())).into(holder.roundedImageView); //视频封面
        holder.textView_titile.setText(videoEntities.get(position).getVideo_name()); //视频标题
        holder.textView_1.setText(videoEntities.get(position).getUser_name()); //视频发布者
        holder.textView_2.setText(String.valueOf(videoEntities.get(position).getTotalplay())); //视频播放量
        holder.textView_3.setText(String.valueOf(videoEntities.get(position).getComment_count())); //评论数量
        holder.list_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoEntities.get(position).getVideo_type().equals("综艺")||
                        videoEntities.get(position).getVideo_type().equals("电视剧")||
                        videoEntities.get(position).getVideo_type().equals("电影")||
                        videoEntities.get(position).getVideo_type().equals("动漫"))
                {
                    Intent intent=new Intent(context, VideoPinkcat.class);
                    intent.putExtra("v_id",videoEntities.get(position).getVideo_id());
                    intent.putExtra("up_id",videoEntities.get(position).getUser_id());
                    intent.putExtra("v_link",videoEntities.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoEntities.get(position).getVideo_lab());
                    context.startActivity(intent);
                }
                else {
                    Intent intent=new Intent(context, VideoActivity.class);
                    intent.putExtra("v_id",videoEntities.get(position).getVideo_id());
                    intent.putExtra("up_id",videoEntities.get(position).getUser_id());
                    intent.putExtra("v_link",videoEntities.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoEntities.get(position).getVideo_lab());
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return videoEntities.size();
    }
}
